/*jslint browser: true, cap: false, glovar: true, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

/*
    Client-side Sorting Module for table Elements

    Based on code by Michael Synovic
    http://m.synovic.home.att.net/code/tablesort.html

    Note that one and only one tbody element is required.
*/

(function(){

    SONGHAY.ui.TableSorter =
    {

        buildTable:function(vTableId,oRowArray)
        {
            var oTable = document.getElementById(vTableId);
            var oTbody = oTable.tBodies[0];
            var oTbodyClone = oTbody.cloneNode(false);
            var vNumberOfColumns = oRowArray[0].cellCount;

            for (var x=0;x<oRowArray.length;x++)
            {
                var oRow = document.createElement("tr");
                for (var i=0;i<vNumberOfColumns;i++)
                {
                    var oCell = document.createElement("td");
                    oCell.innerHTML = oRowArray[x].getCellData(i);
                    oRow.appendChild(oCell);
                }
                oTbodyClone.appendChild(oRow);
            }
            oTable.replaceChild(oTbodyClone, oTbody);
        },

        generateTableRowArray:function(vTableId)
        {

            var oData = [];
            var oTable = document.getElementById(vTableId);
            var oTbody = oTable.tBodies[0];
            var oRows = oTbody.rows;
            for (var x=0;x<oRows.length;x++)
            {
                var oCells = oRows[x].cells;
                oData.push(new self.TableRowData(oCells));
            }
            return oData;
        },

        getSortDirection:function(vTableId,vColumnIndex)
        {
            var oTable = document.getElementById(vTableId);

            if(oTable)
            {
                oTable.rasxActiveSort = null;
            }
            else { return null; }

            var oActiveSort = oTable.rasxActiveSort;

            if (oActiveSort === null || oActiveSort.column != vColumnIndex)
            {
                oActiveSort = new self.SortReference(vColumnIndex, "asc");
            }
            else
            {
                if (oActiveSort.direction == "asc")
                {
                    oActiveSort.direction = "dsc";
                }
                else
                {
                    oActiveSort.direction = "asc";
                }
            }

            oTable.rasxActiveSort = oActiveSort;

            return oActiveSort.direction;
        },

        sortBy:function(vTableId,vColumnIndex)
        {
            if(arguments.length > 2)
            {
                var vDisableSort = arguments[2];
                if(vDisableSort) { return; }
            }

            var oRowArray = this.generateTableRowArray(vTableId);

            for (var i=0;i<oRowArray.length;i++)
            {
                oRowArray[i].setSortBy(vColumnIndex);
            }

            var oDirection = this.getSortDirection(vColumnIndex);

            oRowArray.sort();

            if (oDirection == "dsc") { oRowArray.reverse(); }

            this.buildTable(vTableId,oRowArray);

        },

        // Data objects for new-ing:

        SortReference:function(vColumnIndex, vDirection)
        {
            this.column = vColumnIndex;
            this.direction = vDirection;
        },

        TableRowData:function(oCells)
        {
            this.cellCount = oCells.length;

            this.getCellData = function(vColumnIndex){ return  oCells[vColumnIndex].innerHTML; };

            //The sort is case-sensitive so toLowerCase() is needed:
            this.setSortBy = function(vColumnIndex){ this.sortBy = oCells[vColumnIndex].innerHTML.toLowerCase(); };

            //override toString():
            this.toString = function (){ return this.sortBy; };
        }
    };

    //Module-level "this":
    var self = SONGHAY.ui.TableSorter;

})();
